import tkinter as tk
from tkinter import font, messagebox
import random

default_font = "微软雅黑"
software_version = "V1.0.2"

class DishSelector:
    def __init__(self, master):
        self.master = master
        master.title("今天吃什么" + " " + software_version)
        master.geometry("600x400")
        try:
            master.iconbitmap('myicon_32.ico')
        except:
            pass
        master.configure(bg="")

        # 修改后的菜单创建
        self._create_direct_menu()

        # 加载字体
        self.title_font = font.Font(family=default_font, size=24, weight="bold")
        self.button_font = font.Font(family=default_font, size=14)
        self.result_font = font.Font(family=default_font, size=18, weight="bold")

        # 创建渐变背景
        self.canvas = tk.Canvas(master, bg="#EFEFEF", highlightthickness=0)
        self.canvas.pack(fill=tk.BOTH, expand=True)
        #self._draw_gradient("#FFEECC", "#FFDDAA")

        # 界面元素容器（修复背景颜色）
        self.main_frame = tk.Frame(self.canvas, bg="#EFEFEF")  # 明确设置背景色
        self.main_frame.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 标题（修复背景颜色）
        self.title_label = tk.Label(self.main_frame, 
                                  text="人是铁饭是钢，一顿不吃饿得慌",
                                  font=self.title_font,
                                  fg="#E74C3C",
                                  bg="#EFEFEF")  # 添加背景色
        self.title_label.pack(pady=20)

        # 结果展示框（保持原有样式）
        self.result_frame = tk.Frame(self.main_frame, 
                                   bg="#EFEFEF",
                                   bd=0,
                                   highlightthickness=2,
                                   highlightbackground="#E0E0E0")
        self.result_frame.pack(pady=15)

        self.result_label = tk.Label(self.result_frame,
                                   text="看来你又在探讨人生一大哲理了。",
                                   font=self.result_font,
                                   fg="#2C3E50",
                                   bg="#EFEFEF",
                                   width=26,
                                   height=2)
        self.result_label.pack(padx=20, pady=15)

        # 动态装饰线
        #self.canvas.create_line(50, 100, 400, 100, fill="#E74C3C", width=2, dash=(4,2))

        # 动画按钮
        self.button = tk.Button(self.main_frame,
                              text="看什么看，点啊！",
                              command=self.toggle_selection,
                              font=self.button_font,
                              bg="#E74C3C",
                              fg="white",
                              activebackground="#C0392B",
                              bd=0,
                              padx=30,
                              pady=10,
                              relief=tk.FLAT)
        self.button.pack(pady=20)

        # 滚动状态
        self.is_rolling = False
        self.rolling_id = None

        # 菜品数据
        try:
            with open("dishes.txt", "r", encoding="utf-8") as f:
                self.dishes = [line.strip() for line in f if line.strip()]
        except FileNotFoundError:
            self.dishes = ["你啥都不跟我说，我咋知道你吃啥"]

    def _draw_gradient(self, color1, color2):
        width = self.master.winfo_width()
        height = self.master.winfo_height()
        for i in range(height):
            r = int((i/height)*153 + (1-i/height)*255)
            g = int((i/height)*204 + (1-i/height)*221)
            b = int((i/height)*255 + (1-i/height)*170)
            color = f"#{r:02x}{g:02x}{b:02x}"
            self.canvas.create_line(0, i, width, i, fill=color)

    def toggle_selection(self):
        if not self.is_rolling:
            # 开始滚动
            self.is_rolling = True
            self.button.config(text="你不点我能停吗？")
            self.start_rolling()
        else:
            # 停止滚动
            self.is_rolling = False
            self.button.config(text="看什么看，点啊！")
            if self.rolling_id:
                self.master.after_cancel(self.rolling_id)

    def start_rolling(self):
        def update():
            if self.is_rolling:
                current = random.choice(self.dishes)
                self.result_label.config(text=current)
                # 动态颜色变化
                r = random.randint(0, 255)
                self.result_label.config(fg=f"#{r:02x}00{(255-r):02x}")
                self.rolling_id = self.master.after(50, update)
        update()

    def _create_direct_menu(self):
        # 创建主菜单栏
        menu_bar = tk.Menu(self.master)

        # 直接添加关于命令到主菜单
        menu_bar.add_command(
            label="关于", 
            command=self.show_about,
            font=font.Font(family=default_font, size=10)
        )

        # 配置到主窗口
        self.master.config(menu=menu_bar)

    def show_about(self):
        about_content = \
        """
        ---->
        版本：1.0.2
        开发者：一天两根冰棍
        发布日期：2025-02-22

        使用说明：
        1. 请在同目录放置dishes.txt文件
        2. 每行输入一个菜品名称
        3. 点击按钮开始滚动，再次点击停止

        修订记录：
        1. 将自动停止滚动改为手动点击停止。

        特别注意：
        王CC ❤ 伏伏
        2025.02.09

        """
        messagebox.showinfo("程序信息", about_content.strip())

if __name__ == "__main__":
    root = tk.Tk()
    app = DishSelector(root)
    root.mainloop()